package com.axend.aerosense.room.viewmodel;

import android.app.Activity;

import com.alibaba.android.arouter.launcher.ARouter;
import com.axend.aerosense.base.bean.BaseDevInfoBean;
import com.axend.aerosense.base.bean.BaseStringBean;
import com.axend.aerosense.base.impl.IBaseView;
import com.axend.aerosense.base.impl.ILoadDataResult;
import com.axend.aerosense.base.impl.IOperationDetailResult;
import com.axend.aerosense.base.livedatabus.LiveDatabus;
import com.axend.aerosense.base.storage.MmkvHelper;
import com.axend.aerosense.base.viewmodel.CustomBaseViewModel;
import com.axend.aerosense.common.UrlApi;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.common.router.RouterActivityPath;
import com.axend.aerosense.common.ui.LoadingDialog;
import com.axend.aerosense.common.util.JsonUtil;
import com.axend.aerosense.network.EasyHttp;
import com.axend.aerosense.network.cache.model.CacheMode;
import com.axend.aerosense.network.callback.ProgressDialogCallBack;
import com.axend.aerosense.network.callback.SimpleCallBack;
import com.axend.aerosense.network.exception.ApiException;
import com.axend.aerosense.room.R;
import com.axend.aerosense.room.entity.RoomInfoReq;
import com.axend.aerosense.room.entity.RoomListBean;
import com.axend.aerosense.room.entity.RoomSortListBean;
import com.blankj.utilcode.util.ToastUtils;

import io.reactivex.disposables.Disposable;

public class RoomEditViewMode extends CustomBaseViewModel<RoomSortListBean> {

    private Disposable disposable;
    private String token;

    public RoomEditViewMode(IBaseView baseViewImpl, ILoadDataResult<RoomSortListBean> loadDataResultImpl) {
        super(baseViewImpl, loadDataResultImpl);
        token = MmkvHelper.getInstance().getMmkv().decodeString(Gobal.SAVE_KEY.TOKEN);
    }

    //设置排序号的房间
    public void setAllRoomSort(Activity activity, RoomListBean roomListBean){
        new BaseDevInfoBean();
        disposable = EasyHttp.post(UrlApi.URL.SetAllRoomSort.getUrl())
                .headers("token", token)
                .cacheMode(CacheMode.NO_CACHE)
                .upJson(JsonUtil.toJson(roomListBean))
                .execute(new ProgressDialogCallBack<BaseStringBean>(LoadingDialog.getProgressDialog(activity)) {
                    @Override
                    public void onTokenExpire() {
                        dismissProgress();
                        ToastUtils.showShort(R.string.common_login_first);
                        ARouter.getInstance().build(RouterActivityPath.Login.PAGE_LOGIN_HOME).withBoolean("jumpMainPage", true).greenChannel().navigation();
                    }

                    @Override
                    public void onError(ApiException e) {
                        dismissProgress();
                        ToastUtils.showShort(e.getMessage());
                    }

                    @Override
                    public void onSuccess(BaseStringBean bean) {
                        LiveDatabus.getInstance().with(Gobal.EVENT_KEY.REFESH_ROOM, Integer.class).postValue(1);
                        dismissProgress();
                        ToastUtils.showShort(R.string.common_success);
                        activity.finish();
                    }
                });
    }

    public void deleteRoom(Activity activity, String uuid, IOperationDetailResult operationResultListence){
        RoomInfoReq bean = new RoomInfoReq(uuid);
        disposable = EasyHttp.post(UrlApi.URL.DelRoom.getUrl())
                .headers("token", token)
                .cacheMode(CacheMode.NO_CACHE)
                .upJson(JsonUtil.toJson(bean))
                .execute(new ProgressDialogCallBack<BaseStringBean>(LoadingDialog.getProgressDialog(activity)) {
                    @Override
                    public void onTokenExpire() {
                        dismissProgress();
                        if(operationResultListence != null){
                            operationResultListence.onFail(Gobal.ERROR_CODE.ERR_LOGIN, "");
                        }
                    }

                    @Override
                    public void onError(ApiException e) {
                        dismissProgress();
                        if(operationResultListence != null){
                            operationResultListence.onFail(e.getCode(), e.getMessage());
                        }
                    }

                    @Override
                    public void onSuccess(BaseStringBean bean) {
                        dismissProgress();
                        ToastUtils.showShort(R.string.common_success);
                        if(operationResultListence != null){
                            operationResultListence.onSuccess("");
                        }
                    }
                });
    }

    @Override
    public void onLoad() {
        disposable = EasyHttp.post(UrlApi.URL.GetEditRoom.getUrl())
                .headers("token", token)
                .cacheMode(CacheMode.NO_CACHE)
                .upJson(JsonUtil.toJson(new BaseDevInfoBean()))
                .execute(new SimpleCallBack<RoomSortListBean>() {

                    @Override
                    public void onTokenExpire() {
                        loadDataFail("");
                        ToastUtils.showShort(R.string.common_login_first);
                        ARouter.getInstance().build(RouterActivityPath.Login.PAGE_LOGIN_HOME).withBoolean("jumpMainPage", true).greenChannel().navigation();
                    }

                    @Override
                    public void onError(ApiException e) {
                        loadDataFail(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RoomSortListBean bean) {
                        loadDataSuccess(bean);
                    }
                });
    }

    @Override
    protected void onCleared() {
        if(null != disposable && !disposable.isDisposed()){
            disposable.dispose();
            disposable = null;
        }
        super.onCleared();
    }
}
